<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<!-- Copyright 1997 The Open Group, All Rights Reserved -->
<title>diff</title>
</head><body bgcolor=white>
<center>
<font size=2>
The Single UNIX &reg; Specification, Version 2<br>
Copyright &copy; 1997 The Open Group

</font></center><hr size=2 noshade>
<h4><a name = "tag_001_014_415">&nbsp;</a>NAME</h4><blockquote>
diff - compare two files
</blockquote><h4><a name = "tag_001_014_416">&nbsp;</a>SYNOPSIS</h4><blockquote>
<pre><code>

diff <b>[</b>-c| -e| -f| -C <i>n</i><b>][</b>-br<b>]</b><i> file1 file2</i>
</code>
</pre>
</blockquote><h4><a name = "tag_001_014_417">&nbsp;</a>DESCRIPTION</h4><blockquote>
The
<i>diff</i>
utility will compare the contents of
<i>file1</i>
and
<i>file2</i>
and write to standard output
a list of changes necessary to convert
<i>file1</i>
into
<i>file2</i>.
This list should be minimal.
No output will be produced if the files are identical.
</blockquote><h4><a name = "tag_001_014_418">&nbsp;</a>OPTIONS</h4><blockquote>
The
<i>diff</i>
utility supports the <b>XBD</b> specification, <a href="../xbd/utilconv.html#usg"><b>Utility Syntax Guidelines</b>&nbsp;</a> .
<p>
The following options are supported:
<dl compact>

<dt><b>-b</b>
<dd>Cause any amount of white space at the end
of a line to be treated as a single
newline character
(that is, the white-space characters preceding the
newline character
are ignored) and
other strings of white-space characters, not including
newline characters,
to compare equal.

<dt><b>-c</b>
<dd>Produce output in a form that provides three lines of context.

<dt><b>-C&nbsp;</b><i>n</i>
<dd>Produce output in a form that provides
<i>n</i>
lines of context
(where
<i>n</i>
will be interpreted as a positive decimal integer).

<dt><b>-e</b>
<dd>Produce output in a form suitable as input for the
<i><a href="ed.html">ed</a></i>
utility,
which can then be used to convert
<i>file1</i>
into
<i>file2</i>.

<dt><b>-f</b>
<dd>Produce output in an alternative form,
similar in format to
<b>-e</b>,
but unsuitable as input for the
<i><a href="ed.html">ed</a></i>
utility, and in the opposite order.

<dt><b>-r</b>
<dd>Apply
<i>diff</i>
recursively to files and directories of the same name when
<i>file1</i>
and
<i>file2</i>
are both directories.

</dl>
</blockquote><h4><a name = "tag_001_014_419">&nbsp;</a>OPERANDS</h4><blockquote>
The following operands are supported:
<dl compact>

<dt><i>file1</i><dd>
<dt><i>file2</i><dd>A pathname of a file be compared.
If either the
<i>file1</i>
or
<i>file2</i>
operand is "-", the standard input will be used in its place.

</dl>
<p>
If both
<i>file1</i>
and
<i>file2</i>
are directories,
<i>diff</i>
will not compare block special files, character special files or
FIFO special files to any files and
will not compare regular files to directories.
The system documentation will specify the behaviour of
<i>diff</i>
on implementation-dependent file types not
specified by the <b>XSH</b> specification when found in directories.
Further details are as specified in
<xref href=diffdir><a href="#tag_001_014_424_001">
Diff Directory Comparison Format
</a></xref>.
<p>
If only one of
<i>file1</i>
and
<i>file2</i>
is a directory,
<i>diff</i>
will be applied to the non-directory file and the file contained
in the directory file with a filename that is the same as the last
component of the non-directory file.
</blockquote><h4><a name = "tag_001_014_420">&nbsp;</a>STDIN</h4><blockquote>
The standard input will be used only if one of the
<i>file1</i>
or
<i>file2</i>
operands references standard input.
See the INPUT FILES section.
</blockquote><h4><a name = "tag_001_014_421">&nbsp;</a>INPUT FILES</h4><blockquote>
The input files must be text files.
</blockquote><h4><a name = "tag_001_014_422">&nbsp;</a>ENVIRONMENT VARIABLES</h4><blockquote>
The following environment variables affect the execution of
<i>diff</i>:
<dl compact>

<dt><i>LANG</i><dd>Provide a default value for the internationalisation variables
that are unset or null.
If
<i>LANG</i>
is unset or null, the corresponding value from the
implementation-dependent default locale will be used.
If any of the internationalisation variables contains an invalid setting, the
utility will behave as if none of the variables had been defined.

<dt><i>LC_ALL</i><dd>
If set to a non-empty string value,
override the values of all the other internationalisation variables.

<dt><i>LC_CTYPE</i><dd>
Determine the
locale for the interpretation of sequences of bytes of text data as
characters (for example, single- as opposed to multi-byte characters
in arguments and input files).

<dt><i>LC_MESSAGES</i><dd>
Determine the locale that should be used to affect
the format and contents of diagnostic
messages written to standard error
and informative messages written to standard output.

<dt><i>LC_TIME</i><dd>
Determine the locale for affecting
the format of file timestamps written with the
<b>-C</b>
and
<b>-c</b>
options.

<dt><i>NLSPATH</i><dd>
Determine the location of message catalogues
for the processing of
<i>LC_MESSAGES .
</i>
<dt><i>TZ</i><dd>Determine the locale for affecting
the timezone used for calculating file timestamps written with the
<b>-C</b>
and
<b>-c</b>
options.

</dl>
</blockquote><h4><a name = "tag_001_014_423">&nbsp;</a>ASYNCHRONOUS EVENTS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_001_014_424">&nbsp;</a>STDOUT</h4><blockquote>
<h5><a name = "tag_001_014_424_001">&nbsp;</a>Diff Directory Comparison Format</h5>
<xref type="5" name="diffdir"></xref>
If both
<i>file1</i>
and
<i>file2</i>
are directories,
the following output formats will be used.
<p>
In the POSIX locale,
each file that is present in only one directory
will be reported using the following format:
<p><code>
<tt>"Only in %s: %s\n"</tt>, &lt;<i>directory pathname</i>&gt;,
&lt;<i>filename</i>&gt;
</code>
<p>
In the POSIX locale,
subdirectories that are common to the two directories
may be reported with the following format:
<p><code>
<pre>
<tt>"Common subdirectories: %s and %s\n"</tt>, &lt;<i>directory1 pathname</i>&gt;,
&lt;<i>directory2 pathname</i>&gt;
</pre>
</code>
<p>
For each file common to the two directories
if the two files are not to be compared,
the following format shall be used in the POSIX locale:
<p><code>
<pre>
<tt>"File %s is a %s while file %s is a %s\n"</tt>, &lt;<i>directory1&nbsp;pathname</i>&gt;,
&lt;<i>file&nbsp;type&nbsp;of&nbsp;directory1&nbsp;pathname</i>&gt;,
&lt;<i>directory2&nbsp;pathname</i>&gt;,
&lt;<i>file&nbsp;type&nbsp;of&nbsp;directory2&nbsp;pathname</i>&gt;
</code>
</pre>
<p>
For each file common to the two directories,
if the files are to be compared and
are identical, no output shall be written.
If the two files differ, the following format shall be written:
<p><code>
<tt>"diff %s %s %s\n"</tt>, &lt;<i>diff_options</i>&gt;,
&lt;<i>filename1</i>&gt;,
&lt;<i>filename2</i>&gt;
</code>
<p>
where
&lt;<i>diff_options</i>&gt;
are the options as specified on the command line.
Depending on these options, one of the following
output formats will be used to write the differences.
<p>
All directory pathnames listed in this section will be relative to
the original command line arguments.
All other names of files listed in this section will be filenames
(pathname components).
<h5><a name = "tag_001_014_424_002">&nbsp;</a>Diff Default Output Format</h5>
The default (without
<b>-e</b>,
<b>-f</b>,
<b>-c</b>
or
<b>-C</b>
options)
<i>diff</i>
utility output contains lines of these forms:
<p><code>
<tt>"%da%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;
</code>
<p><code>
<tt>"%da%d,%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;,
&lt;<i>num3</i>&gt;
</code>
<p><code>
<tt>"%dd%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;
</code>
<p><code>
<tt>"%d,%dd%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;,
&lt;<i>num3</i>&gt;
</code>
<p><code>
<tt>"%dc%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;
</code>
<p><code>
<tt>"%d,%dc%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;,
&lt;<i>num3</i>&gt;
</code>
<p><code>
<tt>"%dc%d,%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;,
&lt;<i>num3</i>&gt;
</code>
<p><code>
<tt>"%d,%dc%d,%d\n"</tt>, &lt;<i>num1</i>&gt;,
&lt;<i>num2</i>&gt;,
&lt;<i>num3</i>&gt;,
&lt;<i>num4</i>&gt;
</code>
<p>
These lines resemble
<i><a href="ed.html">ed</a></i>
subcommands to convert
<i>file1</i>
into
<i>file2</i>.
The line numbers before the action letters pertain to
<i>file1</i>;
those after pertain to
<i>file2</i>.
Thus, by exchanging
a
for
d
and
reading the line in reverse order, one can also determine how to convert
<i>file2</i>
into
<i>file1</i>.
As in
<i><a href="ed.html">ed</a></i>,
identical pairs (where <i>num1</i><b>&nbsp;=&nbsp;</b><i>num2</i>)
are abbreviated as a single number.
<p>
Following each of these lines,
<i>diff</i>
will write to standard output
all lines affected in the first file using the format:
<p>
<code>
<tt>"&lt;<img src="../images/delta.gif" border=0>%s"</tt>, &lt;<i>line</i>&gt;
</code>
<p>
and all lines affected in the second file using the format:
<p>
<code>
<tt>"&gt;<img src="../images/delta.gif" border=0>%s"</tt>, &lt;<i>line</i>&gt;
</code>
<p>
If there are lines affected in both
<i>file1</i>
and
<i>file2</i>
(as with the
<b>c</b>
subcommand), the changes are separated with a
line consisting of three hyphens:
<pre>
<code>
"---\n"
</code>
</pre>
<h5><a name = "tag_001_014_424_003">&nbsp;</a>Diff -e Output Format</h5>
With the
<b>-e</b>
option,
a script will be produced that will,
when provided as input to
<i><a href="ed.html">ed</a></i>,
along with an appended
<b>w</b>
(write) command, convert
<i>file1</i>
into
<i>file2</i>.
Only the
<b>a</b>
(append),
<b>c</b>
(change),
<b>d</b>
(delete),
<b>i</b>
(insert), and
<b>s</b>
(substitute) commands of
<i><a href="ed.html">ed</a></i>
will be used in this script.
Text lines, except those consisting of the single character period
(.),
will be output as they appear in the file.
<h5><a name = "tag_001_014_424_004">&nbsp;</a>Diff -f Output Format</h5>
With the
<b>-f</b>
option,
an alternative format of script will be produced.
It will be similar to that produced by
<b>-e</b>,
with the following differences:
<ol>
<p>
<li>
It will be expressed in reverse sequence;
the output of
<b>-e</b>
will order changes from the end of the file to the beginning;
the
<b>-f</b>
from beginning to end.
<p>
<li>
The command form
&lt;<i>lines</i>&gt;
&lt;<i>command-letter</i>&gt;
used by
<b>-e</b>
will be reversed.
For example,
10c
with
<b>-e</b>
would be
c10
with
<b>-f</b>.
<p>
<li>
The form used for ranges of line numbers will be
space-character-separated,
rather than comma-separated.
<p>
</ol>
<h5><a name = "tag_001_014_424_005">&nbsp;</a>Diff -c or -C Output Format</h5>
With the
<b>-c</b>
or
<b>-C</b>
option, the output format will consist of affected lines along with
surrounding lines of context.
The affected lines will show which
ones need to be deleted or changed in
<i>file1</i>,
and those
added from
<i>file2</i>.
With the
<b>-c</b>
option,
three lines of context,
if available, will be written before and
after the affected lines.
With the
<b>-C</b>
option, the user
can specify how many lines of context will be written.
The exact format follows.
<p>
The name and last modification time
of each file will be output in the following format:
<p><code>
<pre>
<tt>"*** %s %s\n"</tt>, <i>file1</i>,
&lt;<i>file1&nbsp;time&nbsp;stamp</i>&gt;
<tt>"--- %s %s\n"</tt>, <i>file2</i>,
&lt;<i>file2&nbsp;time&nbsp;stamp</i>&gt;
</code>
</pre>
and a string of 15 asterisks:
<pre>
<code>
"***************\n"
</code>
</pre>
<p>
Each
&lt;<i>file</i>&gt;
field will be the pathname of the corresponding file being compared.
The pathname written for standard input is unspecified.
<p>
In the POSIX locale, each
&lt;<i>timestamp</i>&gt;
field will be equivalent to the
output from the following command:
<pre>
<code>
date "+%a %b %e %T %Y"
</code>
</pre>
without the trailing
newline character,
executed at the time of last modification
of the corresponding file
(or the current time, if the file is standard input).
<p>
Then, the following output formats will be applied for every set of changes.
<p>
First, the range of lines in
<i>file1</i>
will be written in the following format:
<p><code>
<tt>"*** %d,%d ****\n"</tt>, &lt;<i>beginning&nbsp;line&nbsp;number</i>&gt;,
&lt;<i>ending&nbsp;line&nbsp;number</i>&gt;
</code>
<p>
Next, the affected lines along with lines of context (unaffected lines)
will be written.
Unaffected lines will be written in the following format:
<p><code>
<tt>"<img src="../images/delta.gif" border=0>%s"</tt>, &lt;<i>unaffected_line</i>&gt;
</code>
<p>
Deleted lines will be written as:
<p><code>
<tt>"-<img src="../images/delta.gif" border=0>2%s"</tt>, &lt;<i>deleted_line</i>&gt;
</code>
<p>
Changed lines will be written as:
<p><code>
<tt>"!<img src="../images/delta.gif" border=0>%s"</tt>, &lt;<i>changed_line</i>&gt;
</code>
<p>
Next, the range of lines in
<i>file2</i>
will be written in the following format:
<p><code>
<tt>"--- %d,%d ----\n"</tt>, &lt;<i>beginning line number</i>&gt;,
&lt;<i>ending line number</i>&gt;
</code>
<p>
Then, lines of context and changed lines will be written as described
in the previous formats.
Lines added from
<i>file2</i>
will be written in the following format:
<p><code>
<tt>"+<img src="../images/delta.gif" border=0>%s"</tt>, &lt;<i>added_line</i>&gt;
</code>
</blockquote><h4><a name = "tag_001_014_425">&nbsp;</a>STDERR</h4><blockquote>
Used only for diagnostic messages.
</blockquote><h4><a name = "tag_001_014_426">&nbsp;</a>OUTPUT FILES</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_427">&nbsp;</a>EXTENDED DESCRIPTION</h4><blockquote>
None.
</blockquote><h4><a name = "tag_001_014_428">&nbsp;</a>EXIT STATUS</h4><blockquote>
The following exit values are returned:
<dl compact>

<dt>0<dd>No differences were found.

<dt>1<dd>Differences were found.

<dt>&gt;1<dd>An error occurred.

</dl>
</blockquote><h4><a name = "tag_001_014_429">&nbsp;</a>CONSEQUENCES OF ERRORS</h4><blockquote>
Default.
</blockquote><h4><a name = "tag_001_014_430">&nbsp;</a>APPLICATION USAGE</h4><blockquote>
If lines at the end of a file are changed and other lines are added,
<i>diff</i>
output may show this as a delete and add, as a change, or as a change and
add;
<i>diff</i>
is not expected to know which happened and users should not
care about the difference in output as long as it clearly shows the
differences between the files.
</blockquote><h4><a name = "tag_001_014_431">&nbsp;</a>EXAMPLES</h4><blockquote>
If
<b>dir1</b>
is a directory containing a directory named
<b>x</b>,
<b>dir2</b>
is a directory containing a directory named
<b>x</b>,
<b>dir1/x</b>
and
<b>dir2/x</b>
both contain files named
<b>date.out</b>,
and
<b>dir2/x</b>
contains a file named
<b>y</b>,
the command:
<pre>
<code>
diff -r dir1 dir2
</code>
</pre>
could produce output similar to:
<pre>
<code>
Common subdirectories: dir1/x and dir2/x
Only in dir2/x: y
diff -r dir1/x/date.out dir2/x/date.out
1c1
&lt; Mon Jul  2 13:12:16 PDT 1990
---
&gt; Tue Jun 19 21:41:39 PDT 1990
</code>
</pre>
</blockquote><h4><a name = "tag_001_014_432">&nbsp;</a>FUTURE DIRECTIONS</h4><blockquote>
The IEEE PASC 1003.2 Interpretations Committee has forwarded concerns about 
parts of this interface definition to the IEEE PASC Shell and Utilities Working Group
which is identifying the corrections.
A future revision of this specification will align with
IEEE Std. 1003.2b when finalised.
</blockquote><h4><a name = "tag_001_014_433">&nbsp;</a>SEE ALSO</h4><blockquote>
<i><a href="cmp.html">cmp</a></i>,
<i><a href="comm.html">comm</a></i>,
<i><a href="dircmp.html">dircmp</a></i>,
<i><a href="ed.html">ed</a></i>.
<br>
</blockquote><hr size=2 noshade>
<center><font size=2>
UNIX &reg; is a registered Trademark of The Open Group.<br>
Copyright &copy; 1997 The Open Group
<br> [ <a href="../index.html">Main Index</a> | <a href="../xshix.html">XSH</a> | <a href="../xcuix.html">XCU</a> | <a href="../xbdix.html">XBD</a> | <a href="../cursesix.html">XCURSES</a> | <a href="../xnsix.html">XNS</a> ]

</font></center><hr size=2 noshade>
</body></html>
